package org.misalen.web.server.controllers.owner;

import java.util.Date;

import org.misalen.annotations.ControllerApi;
import org.misalen.annotations.InterfaceApi;
import org.misalen.utils.TextUtil;
import org.misalen.web.database.model.mysql.pro.ProSmsCode;
import org.misalen.web.server.config.ErrorCode;
import org.misalen.web.server.config.ServerResult;
import org.misalen.web.server.config.ServerUrl;
import org.misalen.web.server.config.Uncertain;
import org.misalen.web.server.controllers.BaseController;
import org.misalen.web.server.exception.CustomerException;
import org.misalen.web.server.forms.user.SmsForm;
import org.misalen.web.server.result.Empty;
import org.misalen.web.server.util.FormUtil;
import org.misalen.web.service.sms.info.AliInfoType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

@Controller
@ControllerApi(value = "短信接口", description = "", index = 000)
@RequestMapping(value = ServerUrl.OwnerSMSControllerUrl)
public class OwnerSmsController extends BaseController {

	@RequestMapping(value = "/code", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "发送短信验证码", resultClass = Empty.class)
	public @ResponseBody ServerResult login(@RequestBody SmsForm form) throws CustomerException {
		FormUtil.validate(form);
		AliInfoType infoType;
		if (form.getType().equals("find-pwd")) {
			infoType = AliInfoType.ownerFindPwdCode;
		} else{
			infoType = AliInfoType.ownerRegisteredCode;
		}
		long count = proOwnerUserRepository.countByMobile(form.getMobile());
		long sendByTimeCount = proSmsCodeRepository.countMobileAndTime(form.getMobile(), Uncertain.smsFrequency,
				infoType.type);
		ServerResult result = null;
		if (infoType.equals(AliInfoType.ownerRegisteredCode) && count > 0) {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00004);
		} else if (infoType.equals(AliInfoType.ownerFindPwdCode) && count == 0) {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00005);
		} else if (sendByTimeCount > 0) {
			result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00001);
		} else {
			String random = TextUtil.getNonceNum(Uncertain.smsCodeLength);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("code", random);
			// jsonObject.put("time", String.valueOf(Uncertain.smsValidity /
			// 60000));
			boolean isSuccess = aliSms.send(form.getMobile(), infoType, jsonObject);
			if (isSuccess) {
				ProSmsCode record = new ProSmsCode();
				record.setAddTime(new Date());
				record.setMobile(form.getMobile());
				record.setType(infoType.type);
				record.setIsSuccess(TextUtil.booleanToString(isSuccess));
				record.setCode(random);
				proSmsCodeRepository.save(record);
				result = ServerResult.ok();
			} else {
				result = ServerResult.error(ErrorCode.SYS_ERROR_CODE_00002);
			}
		}
		return result;
	}

}
